package com.xi.jetcomposedemo.activity

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

class TestCompositionLocalActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            content()
        }
    }

    @Composable
    private fun content() {
        Column(Modifier.fillMaxSize()) {
            Box(
                Modifier
                    .height(45.dp)
                    .clickable {
                        finish()
                    }, contentAlignment = Alignment.Center
            ) {
                Text(
                    text = "返回", modifier = Modifier.padding(start = 12.dp)
                )
            }
            CompositionLocalProvider(LocalElevations provides CardElevation.high) {
                MyCard(backgroundColor = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.05F)) {

                }
            }

            MyCard(backgroundColor = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.05F)) {

            }
        }
    }
}

private data class Elevations(val card: Dp = 0.dp)

private val LocalElevations = compositionLocalOf { Elevations() }

private object CardElevation{
    val high = Elevations(card = 10.dp)
    val low = Elevations(card = 5.dp)
}

@Composable
private fun MyCard(
    elevation: Dp = LocalElevations.current.card,
    backgroundColor: Color,
    content: @Composable ColumnScope.() -> Unit
) {
    Card(
        elevation = CardDefaults.cardElevation(defaultElevation = elevation),
        modifier = Modifier.size(200.dp),
        colors = CardDefaults.cardColors(containerColor = backgroundColor),
        content = content
    )
}


